/*
 * DriversObjectFactory.java
 *
 * Created on Penktadienis, 2006, Spalio 6, 11.35
 *
 */

package org.mif.mobp.core;
import java.io.*;
import java.util.prefs.Preferences;
import org.mif.mobp.serial.SerialPort;

/**
 * Klase sukurs visus objektus, o driveriai po to gales ish jos viska paiimti
 */
abstract public class DriversObjectFactory {
    
    private static boolean isPort;
    private static String name, driver;
    
    protected static RealCoordRetriever rcr;
    protected static Validator validator;
    protected static DataParser dp;
    protected static DataStream ds;
    protected static java.io.InputStream is;
    protected static SerialPort sp;
    
    protected static boolean isInitialized = false;
    /** Creates a new instance of DriversObjectFactory */
    public DriversObjectFactory() {
    }
    
    public DriversObjectFactory (java.io.InputStream _is, 
                                 DataStream _ds,
                                 DataParser _dp,
                                 Validator _validator,
                                 RealCoordRetriever _rcr) 
    {
        rcr = _rcr;
        validator = _validator;
        dp = _dp;
        ds = _ds;
        is = _is;
        isInitialized = true;
    }
    
    /**Inicializuoja factory. 
     * @param isPort jei <code>true</code> tai naudosime porta
     * @param name priklausomai nuo isPort reikshmes - arba porto vardas, 
     * arba kelias iki failo
     * @param driver draiverio vardas
     */
    public static void initialize (boolean isPort, String name, String driver) 
    {
        Preferences p = Preferences.userRoot().node("mobp");;
        DriversObjectFactory.isPort = isPort;
        DriversObjectFactory.name = name;
        DriversObjectFactory.driver = driver;
        try {
            if (isPort) { //naudojam porta
                if (sp != null) {
                    if (sp.isOpen()) {
                        try {
                            Thread.sleep (300);
                        }
                        catch (InterruptedException ex) {

                        }
                        sp.close();
                    }
                }
                String flowCntrl = org.mif.mobp.gui.PortSettingsDialog.getFlowControl();
                sp = new SerialPort (Integer.parseInt(name.substring(3)),
                        org.mif.mobp.gui.PortSettingsDialog.getBitsPerSecond(),
                        (byte)(org.mif.mobp.gui.PortSettingsDialog.getDataBits()),
                        (byte)(org.mif.mobp.gui.PortSettingsDialog.getStopBits()),
                        (byte)0, //???paryti????
                        flowCntrl.equals("none")?false:true);
                is = sp.getInputStream();

            }
            else { //naudojam faila
                InputStream _is = null;
                try { //sukuriam inputstream'a'
                    _is = new FileInputStream (name);
                }
                catch (FileNotFoundException ex) {
                    ex.printStackTrace();
                    return;
                }
                is = _is;
            }


            if (driver.equals ("org.mif.mobp.cellid")) {
                //Rs232Imitator ri=new Rs232Imitator(is,98, 10);
                ds = new org.mif.mobp.cellid.GsmStream (is);
                dp = new org.mif.mobp.cellid.GsmDataParser (ds);
                rcr = new org.mif.mobp.cellid.GsmCoordRetriever (dp);
                validator = new org.mif.mobp.cellid.GsmValidator();
            } //if
            else if (driver.equals ("org.mif.mobp.gps")) {
                ds = new org.mif.mobp.gps.GpsStream (is);
                dp = new org.mif.mobp.gps.GpsDataParser (ds);
                rcr = new org.mif.mobp.gps.GpsCoordRetriever (dp);
                String v = p.get("ChosenValidator", "BasicValidator");
                if (v.equals("BasicValidator"))
                    validator = new org.mif.mobp.advanced.BasicValidator (56);
                else if (v.equals("AccelValidator"))
                    validator = new org.mif.mobp.advanced.AccelValidator (56);
                else if (v.equals("LinearValidator"))
                    validator = new org.mif.mobp.advanced.LinearValidator (56);
            }//else if


            isInitialized = true;
        }
        catch (Exception ex) {
            isInitialized = false;
            throw new RuntimeException (ex.getMessage());
        }
    }
    
    /** Ish naujo sukuria draiverio objektus su tais paciais parametrais */
    public static void initialize () {
        DriversObjectFactory.initialize (isPort, name, driver);
    }
    
    /** Pasako ar Factory buvo inicializuota*/
    public static boolean isInitialized () {
        return isInitialized;
    }
    
    public static DataParser getDataParser () {
        return dp;
    }
    
    public static DataStream getDataStream () {
        return ds;
    }
    
    public static java.io.InputStream getInputStream () {
        return is;
    }
    
    public static RealCoordRetriever getRealCoordRetriever () {
        return rcr;
    }
    
    public static Validator getValidator () {
        return validator;
    }
    
    
}
